import {
	Entity,
	Column,
	PrimaryGeneratedColumn,
	ManyToOne,
	JoinColumn,
} from "typeorm";
import { Order } from "./order.entity";
import { Product } from "../../product/entities/product.entity";
import { ProductSku } from "../../product/entities/product-sku.entity";
import { Expose } from "class-transformer";

@Entity("order_items")
export class OrderItem {
	@PrimaryGeneratedColumn()
	@Expose()
	id: number;

	@Column()
	@Expose()
	quantity: number;

	@Column()
	@Expose()
	points: number;

	@Column()
	@Expose()
	totalPoints: number;

	// 冗余字段，存储商品图片
	@Column({ nullable: true })
	@Expose()
	image: string;

	// 关系
	@ManyToOne(() => Order, (order) => order.items, { onDelete: "CASCADE" })
	@JoinColumn({ name: "order_id" })
	order: Order;

	@Column()
	@Expose()
	orderId: number;

	@ManyToOne(() => Product, (product) => product.orderItems)
	@JoinColumn({ name: "product_id" })
	@Expose()
	product: Product;

	@Column()
	@Expose()
	productId: number;

	// SKU关系
	@ManyToOne(() => ProductSku)
	@JoinColumn({ name: "sku_id" })
	@Expose()
	sku: ProductSku;

	@Column({ nullable: true })
	@Expose()
	skuId: number;

	// 规格信息冗余字段，存储JSON格式的SKU规格
	@Column({ type: "json", nullable: true })
	@Expose()
	skuSpecs: Record<string, string>;

	// 冗余字段，便于查询
	@Column({ length: 100 })
	@Expose()
	productName: string;
}
